// JavaScript Document
var stationId;
$(function(){
    var id;
    var order;//标记用户点击的信息索引值，为信息的修改删除做定位标记

    //1，点击删除按钮删除相应信息词条
    $('#f-table').on('click','.bg-color2',function(){
        id =$(this).parents('tr').attr('class');
    })

    $('#delete').click(function () {
        $.ajax({
            type: "POST",
            url: "../car/deleteCar",
            dataType:'json',
            data:{order:id},
            success: function(data)
            {
                if (data==0){
                    // alert("删除成功")
                }else if (data==1){
                    alert("删除失败")
                }else if (data==2){
                    alert("当前用户没有该权限")
                } else {
                    alert("系统异常")
                }
                window.location.href = "../car/getCars";
            }
        });
    });

    //添加
    $('#addCar').click(function () {
        var carNumber = $('.num-input').val();             //车牌号
        var stationName = $('.name-input').val();	 		//所属场站
        var stationId   = $('.name-input').attr('name');
        alert(stationId);
        var model = $('.model-input').val();
        var carBrand = $('.owner-input').val();		 //车辆品牌
        var length = $('.coords-input').val();			 //长
        var width = $('.coords-input2').val();		//宽
        var height = $('.coords-input3').val();			//高
        var loud = $('.weight-input').val();		//重
        var car  = {};
        car["stationId"] = stationId;
        car["carNumber"] = carNumber;
        car["stationName"] = stationName;
        car["carBrand"] = carBrand;
        car["length"] = length;
        car["width"] = width;
        car["height"] = height;
        car["loud"] = loud;
        car["model"] = model;
        alert(stationId);
        $.ajax({
            type: "POST",
            url: "../car/addCar",
            dataType:'json',
            data:car,
            success: function(data)
            {
                if (data==0){
                    // alert("添加成功")

                }else if (data==1){
                    alert("添加失败")
                }
                else if (data==3){
                    alert("车牌号有误")
                }
                else if (data==4){
                    alert("车牌号不能为空")
                }else {
                    alert("系统异常")
                }
                window.location.href = "../car/getCars";
            }
        });
    });
    //6，确定修改时定位被修改项：
    $('#f-table').on('click','.rewrite',function(){
        id =$(this).parents('tr').attr('class');
    });

    //修改信息后保存操作：
    $('#updateCar').click(function () {

        var carNumber = $('.num-input').val();             //车牌号
        var stationName = $('.name-input').val();	 		//所属场站
        var stationId   = $('.stationId-input').val();                 //场站id;
        var carBrand = $('.owner-input').val();		 //车辆品牌
        var length = $('.coords-input').val();			 //长
        var width = $('.coords-input2').val();		//宽
        var height = $('.coords-input3').val();			//高
        var loud = $('.weight-input').val();		//重
        var model = $('.model-input').val();
        var car  = {};
        // alert(id);
        car["id"] = id;
        car["carNumber"] = carNumber;
        car["stationName"] = stationName;
        car["stationId"] = stationId;
        car["carBrand"] = carBrand;
        car["length"] = length;
        car["width"] = width;
        car["height"] = height;
        car["loud"] = loud;
        car["model"] = model;
        $.ajax({
            type: "POST",
            url: "../car/updateCar",
            dataType:'json',
            data:car,
            success:function(data){
                if (data.status==200){
                    // alert(data.msg)
                }else {
                    alert(data.msg)
                }
                window.location.href = "../car/getCars";
            }
        });
    });

    // //搜索
    // $('#souSuoCar').click(function () {
    //     alert("1111111"+search);
    //     var search = $('.search-query').val();
    //     location.href="../car/choose?word="+search;
    // });



    //=======================================================================
    //，点击修改/查看，让选择框弹出
    $('#recheck-btn').click(function(){
        $('#pop-bottom').css('display','block');//弹出新建信息页面
        $('#new-count').find('input').val('');//初始化新建信息页面
        $('#new-count').css('display','block');//显示新建信息页面的按钮组
    });

    //关闭修改/查看弹窗
    $('#new-count').on('click','.close',function(){
        $(this).parents('#new-count').hide();
        $('#pop-bottom').css('display','none');
    });
    //点击蒙版关闭修改/查看弹窗
    $('#pop-bottom').click(function(){
        $(this).hide();
        $('#new-count').hide();
    });


    //点击查看/修改弹窗进行选择时的效果
    var n = $('#left-list>dd').length;
    var num = $('#right-list>dd').length;
    $('#left-list>dt').text('已选（'+n+'项)');
    $('#right-list>dt').text('待选（'+num+'项)');
    //点击右侧待选区效果：
    var txts = null;
    $('#right-list').on('click','input',function(){
        if(n == 1)
        {
        }else{
            if($(this).attr('checked')=='checked'){
                var txt =  $(this).parent().text();
                var id  =  $(this).attr("id");
                stationId = id;
                txts = txt;
                var aim = $('<dd class="p-b-10"><input type="checkbox" checked/>'+txt+'</dd>');
                n= ++n;
                num = --num;
               /* $('#left-list').append(aim);
                $('#left-list>dt').text('已选（'+n+'项)');
                $('#right-list>dt').text('待选（'+num+'项)')
                $(this).parent().remove();*/
            }
        }
    });
    //点击左侧已选区效果：
    $('#left-list').on('click','input',function(){
        var txt =  $(this).parent().text();
        var aim = $('<dd class="p-b-10" id="selected"><input type="checkbox" id=station>'+txt+'</dd>');
        n= --n;
        num = ++num;
        $('#right-list').append(aim);
        $('#left-list>dt').text('已选（'+n+'项)');
        $('#right-list>dt').text('待选（'+num+'项)')
        $(this).parent().remove();
    });
    //点击确认添加
    $('#new_add>button').click(function(){
        $('#new-count').hide();
        $('#pop-bottom').hide();
        document.getElementById("carStation").value=txts;
        document.getElementById("carStation").setAttribute('name', stationId);
        document.getElementById("carStationId").value=stationId;

    });


    //==========================================================================


})

function doUploadFile() {
    var formData = new FormData($( "#uploadFormFile" )[0]);
    $.ajax({
        type: 'POST',
        url: '../file/upload' ,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function (returndata) {
            alert(returndata);
            var josn = JSON.stringify(returndata);
            var filePath = returndata.data;
            alert("上传成功");
            $.ajax({
                type: "POST",
                url: "../car/excelSaveCar",
                dataType:formData,
                data:{path:filePath},
                success: function(returndata){
                    var success  = JSON.stringify(returndata);
                    alert(JSON.stringify(returndata));
                },
                error: function (returndata) {
                    alert("文件导入失败");
                }
            });//ajax套用
        },
        error: function (returndata) {
            alert("上传失败");
        }
    });
}

$("input[type='file']").change(function(){
    var path = $('#filePath').val();
    $("#fileid").val(path);
    function getObjectURL(file) {
        var url = null;
        if (window.createObjcectURL != undefined) {
            url = window.createOjcectURL(file);
        } else if (window.URL != undefined) {
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL != undefined) {
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
    filePathTrue = getObjectURL(this.files[0]);//这里的objURL就是input file的真实路径
    //$("#fileid").val(objURL);
});

// $('#souSuoCar2').blur(function () {
//     var search = $('.search-query').val();
//     location.href="../car/choose?word="+search;
// });

// function souSuoCar() {
//     alert("1111111"+search);
//     var search = $('.search-query').val();
//     location.href="../car/choose?word="+search;
// }
function uploadCar(){
    var formData = new FormData($( "#uploadFormFile" )[0]);
    $.ajax({
        type: 'POST',
        url: '../car/exoutIn' ,
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success:function(data){
            alert(data.msg)
            // if (data.status==200){
            //     alert(data.msg)
            // }else {
            //     alert(data.msg)
            // }
            window.location.href = "../car/getCars";
        }
    });
}